/**
 * fileName:  MySQLRealm.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-2-20 下午7:06:16
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2020
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-2-20          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.uphold.extension.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  MySQLRealm
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class MySQLRealm extends AuthorizingRealm {

	/**
	 * 授权方法
	 * @param principals
	 * @return
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {
		
		System.out.println("授权");
		
		Set<String> permissions = new HashSet<String>();
		Set<String> roleNames = new HashSet<String>();
		
		permissions.add("001:00101");
		roleNames.add("admin");
		
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roleNames);
        info.setStringPermissions(permissions);
		
		return info;
	}

	/**
	 * 认证方法
	 * @param token
	 * @return
	 * @throws AuthenticationException
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		System.out.println("MySQL 查询用户");
		
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		System.out.println(upToken.getUsername());
		
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("admin", "1qaz".toCharArray(), "");
		
		return info;
	}

}
